package com.dengqi.mymodule.activity.network.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dengqi.mymodule.R;
import com.module.retrofit.utils.GloadUtil;

import static com.module.retrofit.utils.GloadUtil.STATUS_LOADING;

/**
 * @Author: dengqi
 * @CreateDate: 2020/10/21 15:46
 * @Description:
 */
public class GlobalLayout extends FrameLayout implements IGlobal {

    private Context context ;
    private View gloadView;
    private ProgressBar animProgress;
    private ImageView img;
    private TextView tv;
    private String emptyText;
    private String failedText;
    private int emptyImage = -1;
    private int failedImage = -1;
    private OnClickListener listener;
    private boolean isInterceptTouch = true; //是否拦截事件

    public GlobalLayout(Context context) {
        super(context);
        this.context = context ;
        init();
    }

    public GlobalLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context ;
        init();
    }
    public GlobalLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }
    public void init() {
//        gloadView添加时机
//                只在第一次显示加载

    }

    private void initGloadView(){
        gloadView = LayoutInflater.from(context).inflate(R.layout.view_global, null);
        ViewGroup.LayoutParams lp =  new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        gloadView.setLayoutParams(lp);
        animProgress =  gloadView.findViewById(R.id.animProgress);
        img =  gloadView.findViewById(R.id.img_error_layout);
        tv =  gloadView.findViewById(R.id.tv_error_layout);
        addView(gloadView);
        //默认STATUS_LOADING只显示这一次
        setGloadViewStatus(STATUS_LOADING);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        initGloadView();
    }

    /**
     * 拦截其他控件点击事件
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return isInterceptTouch;
    }

    /**
     * 点击事件
     * @param listener
     */
    public void setOnLayoutClickListener(OnClickListener listener) {
        this.listener = listener;
    }

    //传入不同状态的图片 文字
    public void setFailedImage(int imgResource) {
        failedImage = imgResource;
    }
    public void setFailedText(String text) {
        failedText = text;
    }
    public void setEmptyImage(int imgResource) {
        emptyImage = imgResource;
    }
    public void setEmptyText(String text) {
        emptyText = text;
    }

    @Override
    public void setGloadViewStatus(GloadUtil type) {

        switch (type) {
            case STATUS_LOADING:
                setOnClickListener(null);
                animProgress.setVisibility(View.VISIBLE);
                img.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
                tv.setText("正在加载...");
                gloadView.setVisibility(View.VISIBLE);
                isInterceptTouch = true;
                break;
            case STATUS_EMPTY:
                setOnClickListener(listener);
                animProgress.setVisibility(View.GONE);
                img.setImageResource(emptyImage == -1 ? R.drawable.icon_nodata : emptyImage);
                img.setVisibility(View.VISIBLE);
                tv.setText(emptyText == null ? "点击屏幕，重新加载" : emptyText);
                tv.setVisibility(View.VISIBLE);
                gloadView.setVisibility(View.VISIBLE);
                isInterceptTouch = true;
                break;
            case STATUS_FAILED:
                setOnClickListener(listener);
                animProgress.setVisibility(View.GONE);
                img.setImageResource(failedImage == -1 ? R.drawable.icon_net_tip : failedImage);
                img.setVisibility(View.VISIBLE);
                tv.setText(failedText == null ? "点击屏幕，重新加载" : failedText);
                tv.setVisibility(View.VISIBLE);
                gloadView.setVisibility(View.VISIBLE);
                isInterceptTouch = true;
                break;
            case STATUS_SUCCESS:
                setOnClickListener(null);
                animProgress.setVisibility(View.GONE);
                gloadView.setVisibility(View.GONE);
                isInterceptTouch = false;
                break;
        }
    }


}
